package com.ruoyi.forum.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.forum.constant.BlacklistStatusEnum;
import com.ruoyi.forum.dao.CircleBlacklistDao;
import com.ruoyi.forum.domain.CircleBlacklist;
import com.ruoyi.forum.exception.BizExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * @Description:论坛小黑屋(CircleBlacklist)表服务层
 * @Author: hoda
 * @Date: Create in 2019-09-04
 * @Modified By:
 * @Modified Date:
 */
@Service
public class CircleBlacklistService {

    @Autowired
    private CircleBlacklistDao circleBlacklistDao;

    /**
     * 新增禁言用户
     *
     * @param uid
     * @param days
     * @param reason
     * @param sid
     * @param operator
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(String uid, Integer days, String reason, String sid, String operator) {
        long now = System.currentTimeMillis();

        // 解禁之前记录
        CircleBlacklist oldBlack = getBlackedByUid(uid);
        if (oldBlack != null) {
            oldBlack.setStatus(BlacklistStatusEnum.NO.value());
            oldBlack.setUpdateBy(operator);
            oldBlack.setUpdateTime(now);
            circleBlacklistDao.updateById(oldBlack);
        }

        // 重新禁言
        CircleBlacklist newBlack = new CircleBlacklist();
        newBlack.setUid(uid);
        newBlack.setReason(reason);
        newBlack.setStatus(BlacklistStatusEnum.YES.value());
        newBlack.setReleaseTime(convertReleaseTime(days));
        newBlack.setSid(sid);
        newBlack.setCreateBy(operator);
        newBlack.setCreateTime(now);
        circleBlacklistDao.save(newBlack);
    }

    /**
     * 根据用户id查询禁言信息
     *
     * @param uid
     * @return
     */
    public CircleBlacklist getBlackedByUid(String uid) {


        // 构造查询条件: uid等于指定值 且 status等于BlacklistStatusEnum.YES.value()
        LambdaQueryWrapper<CircleBlacklist> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CircleBlacklist::getUid, uid)
                .eq(CircleBlacklist::getStatus, BlacklistStatusEnum.YES.value());

        // 执行查询并返回第一条记录
        List<CircleBlacklist> blacklists = circleBlacklistDao.list(wrapper);
        if (CollectionUtils.isNotEmpty(blacklists)) {
            return blacklists.get(0);
        }
        return null;
    }

    /**
     * 根据时间类型获取解禁时间
     *
     * @param days
     * @return
     */
    private Long convertReleaseTime(Integer days) {
        return LocalDateTime.now().plusDays(days).toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 解禁用户
     *
     * @param uid
     * @param operator
     */
    public void release(String uid, String operator) {
        CircleBlacklist black = getBlackedByUid(uid);
        if (black != null) {
            black.setStatus(BlacklistStatusEnum.NO.value());
            black.setUpdateBy(operator);
            Long time = System.currentTimeMillis();
            black.setUpdateTime(time);
            black.setReleaseTime(time);
            circleBlacklistDao.updateById(black);
        }
    }

    /**
     * 释放到期得小黑屋用户
     */
    public void releaseExpiredUser() {
        circleBlacklistDao.releaseExpiredUser();
    }

    /**
     * 检查用户是否限制
     *
     * @param uid
     */
    public void checkLimit(String uid) throws Exception {
        CircleBlacklist black = getBlackedByUid(uid);
        if (black != null) {
            throw new Exception(BizExceptionEnum.CIRCLE_BLACKLIST_BANNED.getMessage());
        }
    }

    public Page<CircleBlacklist> page(Page<CircleBlacklist> page, LambdaQueryWrapper<CircleBlacklist> wrapper) {
        return circleBlacklistDao.page(page, wrapper);
    }

    public CircleBlacklist getById(Long id) {
        return circleBlacklistDao.getById(id);
    }
}